var express = require('express');
var router = express.Router();

/* GET TEST index. */
router.get('/',function(req,res,next){
	res.render('test/index');
});

// router.get('/home',function(req,res,next){
// 	if (!req.session.user) {
// 		var message = 'NO USER';
// 		var data = '<div style="margin-bottom: 20px ; color: red;">' + err + '</div>';
// 	}
// 	next();
// });
router.get('/home', function(req, res, next) {
	if (req.session.user) {
		console.log(req.session.user);	
		res.render('test/home',{user: req.session.user});
	}else{
		res.redirect('login');
	}
});

router.get('/login',function(req,res,next){
	res.render('test/login');
});

router.post('/login',function(req,res,next){
	console.log("hello");
	if (req.body.username == 'admin') {
		var user = {username: 'admin'};
		req.session.user = user;
		res.redirect('home');
	}else{user
		res.redirect('login');
	}
});

router.get('/logout',function(req,res,next){
	req.session.user = null;
	res.redirect('home');
});
module.exports = router;
	